package controllers

import (
	"github.com/astaxie/beego"
	"anfang/models"
	"HTanfang/htmodels"
	"strconv"
	"time"
	"strings"
	"github.com/astaxie/beego/validation"
)

type UsersController struct {
	beego.Controller
}

//带了参数导致bootstrap table无法正常使用
func (c *UsersController)HTUsersList_html() {
	str_UserType := c.Ctx.Input.URL()[strings.LastIndex(c.Ctx.Input.URL(), "/") + 1:]
	UserType, _ := strconv.Atoi(str_UserType)
	c.Data["UserType"] = UserType
	c.TplName = "users_list.html"
	c.Render()
}

func (c *UsersController)HTUsersList() {
	UserType, _ := c.GetInt("UserType")
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "Username", "UserType", "Email", "Phone"}
	Users := []models.Users{}
	htmodels.HTUsersGetList(&Users, fileds, UserType, offset, limit, search, "Username", orderby)
	count, _ := htmodels.HTUsersGetCount(search, "Username", UserType, )
	c.Data["json"] = map[string]interface{}{"rows":&Users, "total":count}
	c.ServeJSON()
}

func (c *UsersController)HTUsersDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetUsersDetailInfo(c, Id)
	}
	mytype, _ := c.GetInt("type")
	c.Data["Type"] = mytype
	c.TplName = "users_detail.html"
}

func SetUsersDetailInfo(c *UsersController, Id int64) {
	Users := models.Users{Id:int(Id)}
	htmodels.HTUsersGetDetail(&Users)
	c.Data["Users"] = &Users
}

func (c *UsersController)HTUsersAdd() {
	Users := models.Users{}
	if err := c.ParseForm(&Users); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("createtime")); err != nil {
		Users.CreateTime = time.Now()
	} else {
		Users.CreateTime = time_up
	}

	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("updatetime")); err != nil {
		Users.UpdateTime = time.Now()
	} else {
		Users.UpdateTime = time_up
	}
	Users.Password = GetPassword("123456")

	valide := validation.Validation{}
	if b, err := valide.Valid(&Users); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTUsersAdd(&Users); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/users/detail`, "添加成功！", "", false)
}

func (c *UsersController)HTUsersUpdate() {
	Id, _ := c.GetInt64("Id");
	Users := models.Users{Id:int(Id)}
	if err := htmodels.HTUsersGetDetail(&Users); err != nil {
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！" + err.Error(), "", true)
	}
	if err := c.ParseForm(&Users); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("createtime")); err != nil {
		Users.CreateTime = time.Now()
	} else {
		Users.CreateTime = time_up
	}

	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("updatetime")); err != nil {
		Users.UpdateTime = time.Now()
	} else {
		Users.UpdateTime = time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&Users); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTUsersUpdate(&Users); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/users/list_html` + `/` + strconv.Itoa(Users.UserType), "更新成功！", "", false)
}

func (c *UsersController)HTUsersDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTUsersDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "删除" + strconv.FormatInt(count, 10) + "行")
}

